
import delimited "${datadir_plookup}\ONS_Postcode_Lookup_UK_09_2017.csv", clear 
ren postcode3 postcode6

keep postcode1 postcode2 postcode6 localautho* regioncode regionname

gen pcd = itrim(postcode6)
replace pcd	= regexr(pcd," ","") // quite slow as a command

duplicates drop pcd, force

save "${datadir}\la_region_postcode_lookup", replace


*--- Lookup for outer postcode

gen pcode = regexs(0) if regexm(postcode2, "[A-Z0-9]+")
br postcode2 pcode

keep pcode local*

bysort pcode localauthoritycode: gen mycount = _N
bysort pcode: egen maxcount = max(mycount)

keep if maxcount == mycount

duplicates drop

set seed 42
duplicates drop pcode, force // only 5 obs deleted

drop mycount maxcount

save "0 - Shared Appreciation\data\la_outer_postcode_lookup", replace
